package com.daka.service.impl;

import com.daka.dao.AuctionTradeListDao;
import com.daka.dao.GoodsDao;
import com.daka.entity.AuctionTradeList;
import com.daka.entity.Goods;
import com.daka.service.AuctionTradeListService;
import com.daka.vo.response.ListTeamAuctionVo;
import com.daka.vo.response.ListUserAuctionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/12/28 18:37
 */
@Service("auctionTradeListService")
@SuppressWarnings("unused")
public class AuctionTradeListServiceImpl implements AuctionTradeListService {

    @Resource
    private AuctionTradeListDao auctionTradeListDao;

    @Resource
    private GoodsDao goodsDao;

    @Override
    public int insert(AuctionTradeList record) {
        return auctionTradeListDao.insert(record);
    }

    @Override
    public AuctionTradeList selectByPrimaryKey(Long id) {
        return auctionTradeListDao.selectByPrimaryKey(id);
    }

    @Override
    public List<AuctionTradeList> selectByUserId(Long userId) {
        return auctionTradeListDao.selectByUserId(userId);
    }

    @Override
    public List<AuctionTradeList> selectByStatement(AuctionTradeList record) {
        return auctionTradeListDao.selectByStatement(record);
    }

    @Override
    public int selectByStatementCount(AuctionTradeList record) {
        return auctionTradeListDao.selectByStatementCount(record);
    }

    @Override
    public int updateByPrimaryKeySelective(AuctionTradeList record) {
        return auctionTradeListDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int countAll() {
        return auctionTradeListDao.countAll();
    }

    @Override
    public List<AuctionTradeList> selectAllByGoods(Long goodId) {
        return auctionTradeListDao.selectAllByGoods(goodId);
    }

    @Override
    public List<ListUserAuctionVo> selectUserRank() {
        return auctionTradeListDao.selectUserRank();
    }

    @Override
    public List<ListTeamAuctionVo> selectTeamRank() {
        return auctionTradeListDao.selectTeamRank();
    }

    public void startOnlinePay(){

        List<Goods> aGoodsList = goodsDao.selectByTypeOrderByAmount(2);

        //整理流水状态
        for (int i = 0;i < aGoodsList.size();i++){
            Goods temp = aGoodsList.get(i);
            Goods goods = goodsDao.selectByPrimaryKey(temp.getId());
            AuctionTradeList auctionParam = new AuctionTradeList();
            auctionParam.setGoods(goods.getId());
            auctionParam.setAmount(goods.getMaxAmount());
            auctionParam.setTeam(goods.getMaxTeam());
            List<AuctionTradeList> list = auctionTradeListDao.selectByStatement(auctionParam);
            if(list.size() > 0){

                AuctionTradeList auctionTradeList = auctionTradeListDao.selectByStatement(auctionParam).get(0);

                auctionTradeList.setPayType(2);//修改支付方式为线下
                auctionTradeListDao.updateByPrimaryKeySelective(auctionTradeList);

            }

            if(i < 5){
                goods.setType(100);
                goodsDao.updateByPrimaryKeySelective(goods);
            }else{
                goods.setType(101);
                goodsDao.updateByPrimaryKeySelective(goods);
            }
        }

        //修改所有订单支付开关为可以支付
        auctionTradeListDao.updatePayFlagByType(2, 1);
        auctionTradeListDao.updatePayFlagByType(100,1);
        auctionTradeListDao.updatePayFlagByType(101,1);
        auctionTradeListDao.updatePayFlagByType(1,1);
    }
}
